<template>
  <div class="login">
    <div class="divLogin"></div>
    <div class="divLogin-h"></div>
    <div class="divLogin-w"></div>
    <div class="stars">
      <div class="star"></div>
      <div class="star" style="top: -580px; left: 800px; animation-delay: 3s;"></div>
      <div class="star" style="top: -340px; left: 646px; animation-delay: 2s;"></div>
      <div class="star" style="top: -620px; left: 395px; animation-delay: 4s;"></div>
      <div class="star" style="top: -860px; left: 765px; animation-delay: 1s;"></div>
      <div class="star" style="top: -770px; left: 1200px; animation-delay: 2s;"></div>
      <div class="star" style="top: -1000px; left: 1000px; animation-delay: 1s;"></div>
    </div>
    <div class="loginImg">
      <img src="@/assets/images/login.png"  />
    </div>

    <div class="loginBody">
      <div class="logoImg">
        <img src="@/assets/logo/logo.png" />
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">五洋油运管理系统</h3>
        <el-form-item prop="username">
          <div class="inputTit">账号</div>
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <div class="inputTit">密码</div>
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <div class="inputTit">验证码</div>
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 65%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin"
            style="width:100%;padding: 13px 20px !important;font-size: 16px !important;">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>


  </div>
</template>

<script>
  import {
    getCodeImg
  } from "@/api/login";
  import Cookies from "js-cookie";
  import {
    encrypt,
    decrypt
  } from '@/utils/jsencrypt'

  export default {
    name: "Login",
    data() {
      return {
        codeUrl: "",
        loginForm: {
          username: "admin",
          password: "admin123",
          rememberMe: false,
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [{
            required: true,
            trigger: "blur",
            message: "请输入您的账号"
          }],
          password: [{
            required: true,
            trigger: "blur",
            message: "请输入您的密码"
          }],
          code: [{
            required: true,
            trigger: "change",
            message: "请输入验证码"
          }]
        },
        loading: false,
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode();
      this.getCookie();
    },
    methods: {
      getCode() {
        getCodeImg().then(res => {
          this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
          if (this.captchaEnabled) {
            this.codeUrl = "data:image/gif;base64," + res.data.img;
            this.loginForm.uuid = res.data.uuid;
          }
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {
                expires: 30
              });
              Cookies.set("password", encrypt(this.loginForm.password), {
                expires: 30
              });
              Cookies.set('rememberMe', this.loginForm.rememberMe, {
                expires: 30
              });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({
                path: this.redirect || "/"
              }).catch(() => {});
            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  body {

  }
  .loginImg{
    overflow: hidden;
    height: 100%;
    width: calc(100vw - 500px);
    position: absolute;
    left: 0;
    img{width: 100%;height: 100%;object-fit: cover;}
  }
  .logoImg{
    position: absolute;right:20px;top: 5px;
    height: 50px;width: 50px;
    img{width: 100%;height: 100%;}
  }
  .divLogin {
    width: 0;
    height: 0;
    position: absolute;
    right: 530px;z-index: 99;
    border-top: 50vh solid transparent;
    border-right: 200px solid #fff;
    border-left: 200px solid transparent;
    border-bottom: 50vh solid transparent;
  }
  .divLogin-h{
    width: 0;
    height: 0;
    position: absolute;
    z-index: 9;
    right: calc(530px + 35px);
    border-top: 50vh solid transparent;
    border-right: 200px solid #2a61ff;
    border-left: 200px solid transparent;
    border-bottom: 50vh solid transparent;
  }
  .divLogin-w{
    background: #2a61ff;right: 530px;
    z-index: 9;
    height: 100vh;width: 35px;position: absolute;
  }
  .loginBody {
    width: 560px;
    height: 100vh;
    background: #fff;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'styleFont';
    position: relative;
  }

  .login {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    background: #2a61ff;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    // background-image: url("../assets/images/login-background.jpg");
    // background-size: cover;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;letter-spacing: 1px;text-shadow: 1px 1px 1px rgba(42, 97, 255, 0.5);
    color: #000;font-size: 33px;font-weight: bold;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 500px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 30%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
    width: 100%;
  }

  .stars {
    height: 0
  }

  .star {
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #2a61ff;
    top: 100px;
    left: 400px;
    position: relative;
    -webkit-transform-origin: 100% 0;
    transform-origin: 100% 0;
    -webkit-animation: star-opacity 5s infinite ease-in, star-pos 5s infinite ease-in;
    animation: star-opacity 5s infinite ease-in, star-pos 5s infinite ease-in;
    -webkit-box-shadow: 0 0 5px 5px rgba(42, 97, 255, 0.3);
    box-shadow: 0 0 5px 5px rgba(42, 97, 255, 0.3);
    opacity: 0;
    z-index: 2
  }

  .star:after {
    content: '';
    display: block;
    top: 0;
    left: 4px;
    border: 0 solid #fff;
    border-width: 0 90px 2px;
    border-color: transparent transparent transparent rgba(42, 97, 255, 0.3);
    -webkit-transform: rotate(-45deg) translate3d(1px, 3px, 0);
    transform: rotate(-45deg) translate3d(1px, 3px, 0);
    -webkit-box-shadow: 0 0 1px 0 rgba(42, 97, 255, 0.1);
    box-shadow: 0 0 1px 0 rgba(42, 97, 255, 0.1);
    -webkit-transform-origin: 0% 100%;
    transform-origin: 0% 100%
  }

  	@-webkit-keyframes star-opacity {
  		0% {
  			opacity: 0
  		}

  		40% {
  			opacity: 1
  		}

  		60% {
  			opacity: 1
  		}

  		100% {
  			opacity: 0
  		}
  	}

  	@keyframes star-opacity {
  		0% {
  			opacity: 0
  		}

  		40% {
  			opacity: 1
  		}

  		60% {
  			opacity: 1
  		}

  		100% {
  			opacity: 0
  		}
  	}

  	@-webkit-keyframes star-pos {
  		0% {
  			transform: scale(0) rotate(0) translate3d(0, 0, 0);
  			-webkit-transform: scale(0) rotate(0) translate3d(0, 0, 0);
  			-moz-transform: scale(0) rotate(0) translate3d(0, 0, 0)
  		}

  		100% {
  			transform: scale(1) rotate(0) translate3d(-450px, 450px, 0);
  			-webkit-transform: scale(1) rotate(0) translate3d(-450px, 450px, 0);
  			-moz-transform: scale(1) rotate(0) translate3d(-450px, 450px, 0)
  		}
  	}

  	@keyframes star-pos {
  		0% {
  			transform: scale(0) rotate(0) translate3d(0, 0, 0);
  			-webkit-transform: scale(0) rotate(0) translate3d(0, 0, 0);
  			-moz-transform: scale(0) rotate(0) translate3d(0, 0, 0)
  		}

  		100% {
  			transform: scale(1) rotate(0) translate3d(-450px, 450px, 0);
  			-webkit-transform: scale(1) rotate(0) translate3d(-450px, 450px, 0);
  			-moz-transform: scale(1) rotate(0) translate3d(-450px, 450px, 0)
  		}
  	}
    ::v-deep .el-input input{
      border-top: none !important;
      border-left: none !important;
      border-right: none !important;
      border-radius: 0 !important;
    }
    .inputTit{font-size: 17px;padding-top: 15px;}
</style>
